Skip to main content

Control Flow

These work just as in many other languages so will not go into further detail.

If/Else

if (test expression1) {
// statement(s)
}
else if (test expression2) {
// statement(s)
}
else {
// statement(s)
}

For

for (initializationStatement; testExpression; updateStatement) {
// statements inside the body of loop
}

While

while (testExpression) {
// the body of the loop
}

Do While

do {
// the body of the loop
}
while (testExpression);

Break and Continue

The break statement ends a loop immediately when it is encountered. The continue statement skips the current iteration of a loop and continues with the next iteration.

Goto

Just dont use this.... if you need it you are doing something wrong unless you have a very very special use-case.

#include <stdio.h>
int main(void)
{
int num, i = 1;
printf("Enter the number whose table you want to print?");
scanf("%d", &num);
table:
printf("%d x %d = %d\n", num, i, num * i);
i++;
if (i <= 10)
goto table;
}

Switch

Important to note here is that the values of expression and each constant-expression must have an integral type and a constant-expression must have an unambiguous constant integral value at compile time. Also the break here makes sure that it doesn't fall through to the other statements.

switch (expression) {
case constant-expression-1:
// statements
break;

case constant-expression-t2:
// statements
break;
default:
// default statements
}